package com.snowfield.lb47.lbconfig.service.impl;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.snowfield.lb47.lbconfig.domain.BlackwhiteList;
import com.snowfield.lb47.lbconfig.mapper.BlackwhiteListDetailMapper;
import com.snowfield.lb47.lbconfig.mapper.BlackwhiteListMapper;
import com.snowfield.lb47.lbconfig.service.IBlackwhiteListDetailService;
import com.snowfield.lb47.lbconfig.service.IBlackwhiteListService;
import com.snowfield.lbcommon.config.SnowFieldConfig;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 黑白名单配置Service业务层处理
 *
 * @author snowfield
 * @date 2024-05-24
 */
@Service
public class BlackwhiteListServiceImpl implements IBlackwhiteListService {
    @Autowired
    private BlackwhiteListMapper blackwhiteListMapper;
    @Autowired
    private BlackwhiteListDetailMapper blackwhiteListDetailMapper;
    @Autowired
    private IBlackwhiteListDetailService blackwhiteListDetailService;

    /**
     * 查询黑白名单配置
     *
     * @param id 黑白名单配置主键
     * @return 黑白名单配置
     */
    @Override
    public BlackwhiteList selectBlackwhiteListById(Long id) {
        return blackwhiteListMapper.selectBlackwhiteListById(id);
    }

    /**
     * 查询黑白名单配置列表
     *
     * @param blackwhiteList 黑白名单配置
     * @return 黑白名单配置
     */
    @Override
    public List<BlackwhiteList> selectBlackwhiteListList(BlackwhiteList blackwhiteList) {
        return blackwhiteListMapper.selectBlackwhiteListList(blackwhiteList);
    }

    /**
     * 新增黑白名单配置
     *
     * @param blackwhiteList 黑白名单配置
     * @return 结果
     */
    @Override
    public int insertBlackwhiteList(BlackwhiteList blackwhiteList) {
        int result = blackwhiteListMapper.insertBlackwhiteList(blackwhiteList);
        if (result > 0) {
            blackwhiteListDetailService.createBlackwhiteListFile(blackwhiteList.getId(), blackwhiteList.getListType());
        }
        return result;
    }

    /**
     * 修改黑白名单配置
     *
     * @param blackwhiteList 黑白名单配置
     * @return 结果
     */
    @Override
    public int updateBlackwhiteList(BlackwhiteList blackwhiteList) {
        blackwhiteList.setUpdateTime(DateUtils.getNowDate());
        return blackwhiteListMapper.updateBlackwhiteList(blackwhiteList);
    }

    /**
     * 批量删除黑白名单配置
     *
     * @param ids 需要删除的黑白名单配置主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteBlackwhiteListByIds(Long[] ids) {
        //保持要删除的配置文件
        List<String> blackWhiteListFilePaths = new ArrayList<>();

        //判断黑白名单是否已经分配，若已经分配不可以删除
        for (Long listId : ids) {
            BlackwhiteList blackwhiteList = blackwhiteListMapper.selectBlackwhiteListById(listId);

            if (blackwhiteList == null) {
                continue;
            }
            int count = blackwhiteListMapper.usedBlackwhiteListCount(listId);
            if (count > 0) {
                throw new ServiceException(String.format("%1$s已分配,不能删除", blackwhiteList.getListName()));
            }

            //文件名称默认为：{listType}_id.lst
            String listFileName = String.format("%s-%d.lst", blackwhiteList.getListType(), blackwhiteList.getId());
            String listFilePath = SnowFieldConfig.getBasicConfigPath() + "/blackwhiteList/" + listFileName;
            blackWhiteListFilePaths.add(listFilePath);
        }
        blackwhiteListDetailMapper.deleteBlackwhiteListDetailByListIds(ids);

        int result = blackwhiteListMapper.deleteBlackwhiteListByIds(ids);
        //删除对应的配置文件
        if (result > 0) {
            for (String blackWhiteListFilePath : blackWhiteListFilePaths) {
                File blackWhiteListFile = new File(blackWhiteListFilePath);
                if (blackWhiteListFile.exists()) {
                    FileUtils.deleteQuietly(blackWhiteListFile);
                }
            }
        }

        return result;
    }

    /**
     * 删除黑白名单配置信息
     *
     * @param id 黑白名单配置主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteBlackwhiteListById(Long id) {
        blackwhiteListDetailMapper.deleteBlackwhiteListDetailByListIds(new Long[]{id});
        return blackwhiteListMapper.deleteBlackwhiteListById(id);
    }

    /**
     * 校验黑白名单名称是否唯一
     *
     * @param blackwhiteList 黑白名单
     * @return 结果
     */
    public Boolean checkNameUnique(BlackwhiteList blackwhiteList) {
        Long listId = blackwhiteList.getId();
        BlackwhiteList list = blackwhiteListMapper.selectBlackwhiteListByName(blackwhiteList.getListName());
        if (list != null && !list.getId().equals(listId)) {
            return Boolean.FALSE;
        }
        return Boolean.TRUE;
    }
}
